package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.NewUserActivity;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.NewUserActivityService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description: 新用户赠送
 * @Date: 创建于 14:21 2020/6/5
 */
@RestController
@RequestMapping("/newActivity")
public class NewUserActivityController {
    @Autowired
    private NewUserActivityService newUserActivityService;

    /**
     * 分页
     *
     * @param accountId
     * @param pageNO
     * @param rowCount
     * @return
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer accountId, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {
        List<NewUserActivity> list = newUserActivityService.queryPage(accountId, pageNO, rowCount);
        list.forEach(NewUserActivity::initActivityTimeStr);
        Integer total = newUserActivityService.queryCount(accountId);
        return JsonResult.ok(list, total);
    }

    /**
     * 新增
     *
     * @param newUserActivity
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.NEWUSER, methods = MethodEnum.NEWUSER_ADD)
    public JsonResult add(NewUserActivity newUserActivity) {
        Assert.notNull(newUserActivity.getAccountId(), "ver.adAccountId.notNull");
        if (newUserActivity.getCouponId() == null && newUserActivity.getGiveMoney() == null) {
            throw new BaseException("ver.coupon.or.money.notNull");
        }
        Assert.notNull(newUserActivity.getName(), "ver.name.notNull");
        if (newUserActivity.getCouponId() != null) {
            Assert.notNull(newUserActivity.getCouponCount(), "ver.couponCount.notNull");
            Assert.notNull(newUserActivity.getValidDays(), "ver.validDays.notNull");
        }
        if (StrUtil.isNotEmpty(newUserActivity.getName()) && newUserActivity.getName().length() > 50){
            throw new BaseException("赠送名称过长");
        }
        if (StrUtil.isNotEmpty(newUserActivity.getRemark()) && newUserActivity.getRemark().length() > 50){
            throw new BaseException("赠送备注过长");
        }
        newUserActivityService.insert(newUserActivity);
        return JsonResult.ok();
    }

    /**
     * 修改
     *
     * @param newUserActivity
     * @return
     */
    @RequestMapping("/edit")
    public JsonResult edit(NewUserActivity newUserActivity) {
        if (newUserActivity.getCouponId() != null) {
            Assert.notNull(newUserActivity.getCouponCount(), "ver.couponCount.notNull");
            Assert.notNull(newUserActivity.getValidDays(), "ver.validDays.notNull");
        }
        if (StrUtil.isNotEmpty(newUserActivity.getName()) && newUserActivity.getName().length() > 50){
            throw new BaseException("赠送名称过长");
        }
        if (StrUtil.isNotEmpty(newUserActivity.getRemark()) && newUserActivity.getRemark().length() > 50){
            throw new BaseException("赠送备注过长");
        }
        newUserActivityService.edit(newUserActivity);
        return JsonResult.ok();
    }

    /**
     * 删除
     *
     * @param newuseractivityId
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.NEWUSER, methods = MethodEnum.NEWUSER_DEL)
    public JsonResult del(Integer newuseractivityId) {
        Assert.notNull(newuseractivityId, "ver.applyId.notnull");
        newUserActivityService.del(newuseractivityId);
        return JsonResult.ok();
    }

}
